/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-12-27                                                                                                               k
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */
define(function(require,exports,module){
    var VERSION = '1.0',
        jQuery = $ = require("jquery-1.8.2.min");

    var _overlay,_window,_caption,_content,_dialog,_footer;
    function Dialog(obj,option){
        this.elems = $(obj);
        this.options = $.extend({
            icon:false,
            title:"",
            content:"",
            flat:false,
            shadow:true,
            overlay:true,
            width:"auto",
            height:"auto",
            position:false,
            padding:false,
            draggable:false,
            overlayClickClose:false,
            sysButtons:{
                btnClose:true,
                btnMax:false,
                btnMin:false
            },
            sure:{
                btn:true,
                onSure:$.noop
            },
            clear:{
                btn:true,
                onClear:$.noop
            },
            onShow:function(_dialog){}
        },option);

        this.init();
    }

    Dialog.prototype = {
        init:function(){
            var that = this;
            this.elems.bind('click',function(e){
                that.elem = $(this);
                e.preventDefault();
                that.renderDom();
            })
        },
        renderDom:function(){
            var that = this;
            _overlay = $("<div/>").addClass("metro window-overlay");
            if(this.options.overlay){
                _overlay.css({
                    backgroundColor:'rgba(0,0,0,0.7)'
                });
            }

            _window = $("<div/>").addClass("window");
            _caption = $("<div/>").addClass("caption");
            _content = $("<div/>").addClass("content");
            _footer = $("<div/>").addClass("footer");

            if(this.options.flat){
                _window.addClass("flat");
            }

            if(this.options.shadow){
                _window.addClass("shadow").css("overflow","hidden");
            }


            if(this.options.sure.btn){
                $("<button/>").text("确定").addClass("info").on('click',function(){
                    that.options.sure.onSure.call(that)
                }).appendTo(_footer)
            }

            if(this.options.clear.btn){
                $("<button/>").text("取消").on('click',function(e){
                    that.options.clear.onClear.call(that);
                    e.preventDefault();
                    e.stopPropagation();
                    that.close();
                }).appendTo(_footer)
            }


            if(this.options.sysButtons){
                if(this.options.sysButtons.btnClose){
                    $("<button/>").addClass("btn-close").on("click",function(e){
                        e.preventDefault();
                        e.stopPropagation();
                        that.close();
                    }).appendTo(_caption);
                }

                if(this.options.sysButtons.btnMax){
                    $("<button/>").addClass("btn-max").on("click",function(e){
                        e.preventDefault();
                        e.stopPropagation();

                    }).appendTo(_caption);
                }

                if(this.options.sysButtons.btnMin){
                    $("<button/>").addClass("btn-min").on("click",function(e){
                        e.preventDefault();
                        e.stopPropagation();
                    }).append(_caption);
                }
            }

            if(this.options.icon){
                $(this.options.icon).addClass("icon").appendTo(_caption);
            }

            $("<div/>").addClass("title").html(this.options.title).appendTo(_caption);

            if(typeof this.options.content == 'function'){
                var fContent = this.options.content.call(_content);
                _content.html(fContent);
            }else{
                _content.html(this.options.content);
            }


            _caption.appendTo(_window);
            _content.appendTo(_window);
            _footer.appendTo(_window);
            _window.appendTo(_overlay);

            if(this.options.width != 'auto'){
                _window.css("min-width",this.options.width);
            }

            if(this.options.height!="auto"){
                _window.css("min-height",this.options.height);
            }

            _overlay.hide().appendTo('body').fadeIn('fast');

            _dialog = _window;
            _window.css("position","fixed")
                .css("top",($(window).height() - _dialog.outerHeight())/2)
                .css("left",($(window).width() - _window.outerHeight())/2);

            _window.on("click",function(e){
                e.stopPropagation();
            });

            if(this.options.draggable){

            }

            if(this.options.overlayClickClose){
                _overlay.on('click',function(e){
                    e.preventDefault();
                    that.close();
                });
            }

            this.options.onShow(_dialog);
            this.autoResize();
        },
        content:function(newContent){
            if(newContent){
                _dialog.children(".content").html(newContent);
                this.autoResize();
            }else{
                return _dialog.children('.content').html();
            }
        },
        title:function(newTitle){
            var _title = _dialog.children(".caption").children('.title');

            if(newTitle){
                _title.html(newTitle)
            }else{
                _title.html();
            }
        },
        autoResize:function(){
            var _content = _dialog.children('.content');
            var top = ($(window).height() - _dialog.outerHeight())/2;
            var left = ($(window).width() - _dialog.outerWidth())/2;
            _dialog.css({
                width:_content.outerWidth(),
                height:_content.outerHeight()
            }).css("top",top).css("left",left);
        },
        close:function(){
            var _overlay = _dialog.parent(".window-overlay");
            _overlay.fadeOut(function(){
                $(this).remove();
            });
        }
    };

    return Dialog;
});